package guoanmaker.personal.business.service;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.Date;
import java.util.List;
import java.util.Set;

import javax.annotation.Resource;
import javax.transaction.Transactional;

import org.springframework.stereotype.Service;

import guoanmaker.personal.business.dao.PersonalAppraiseDao;
import guoanmaker.personal.business.model.MessageType;
import guoanmaker.personal.business.model.PersonalAppraise;
import guoanmaker.personal.business.model.PersonalAppraiseStates;
import guoanmaker.personal.business.model.ProvideCommodity;
import guoanmaker.personal.business.model.ProvideOrderForm;
import guoanmaker.personal.business.model.ReturnResult;
import guoanmaker.personal.business.repository.MessageTypeRepository;
import guoanmaker.personal.business.repository.PersonalAppraiseRepository;
import guoanmaker.personal.business.repository.PersonalAppraiseStatesRepository;
import guoanmaker.personal.business.repository.PersonalOrderFormRepository;
import guoanmaker.personal.business.repository.ProvideCommodityRepository;
import guoanmaker.personal.helper.jpa.Criteria;
import guoanmaker.personal.helper.jpa.Restrictions;
/**
 * 
* @ClassName: PersonalAppraiseService 
* @Description: 评价service
* @author Yvonne_MGL
* @date 2017年7月20日 下午3:13:33 
*  
 */

@Service
public class PersonalAppraiseService {
	
	@Resource
	private PersonalAppraiseStatesRepository personalAppraiseStatesRepository;
	
	@Resource
	private PersonalAppraiseRepository personalAppraiseRepository;
	
	@Resource
	private PersonalAppraiseDao personalAppraiseDao;
	
	@Resource
	private PersonalOrderFormRepository personalOrderFormRepository;
	
	@Resource
	private ProvideCommodityRepository provideCommodityRepository;
	
	@Resource
	private OperatorSensitiveWordService operatorSensitiveWordService;
	
	@Resource
	private MessageTypeRepository messageTypeRepository;
	
	/**
	 * 
	* @Description:查询isused=1的评价选项 
	* @param @return
	* @author Yvonne_MGL 
	* @date 2017年7月20日 下午3:15:52     
	* @return List<PersonalAppraiseStates>    
	* @throws 
	 */
	public List<PersonalAppraiseStates> findAppraiseStatesIsUsed(){
		return personalAppraiseStatesRepository.findAppraiseStatesIsUsed();
	}
	
	/**
	 * 
	* @Description: 保存订单评价信息 
	* @param orderid 订单id
	* @param info 评价分数及类别，格式为  评价类别名称@分数,评价类别名称@分数,评价类别名称@分数,...
	* @param appraise 评语
	* @param @return
	* @author Yvonne_MGL 
	* @date 2017年7月20日 下午4:49:31     
	* @return ReturnResult    
	* @throws 
	 */
	@Transactional
	public ReturnResult saveAppraise(String orderid,String info,String appraise){
		ReturnResult returnResult=new ReturnResult();
		List<PersonalAppraise> appraises=personalAppraiseDao.findByOrderId(orderid);
		if(appraises!=null&&appraises.size()>0){
			returnResult.setKey("error");
			returnResult.setValue("已评价过");
		}else{
			ProvideOrderForm orderForm=personalOrderFormRepository.findOne(orderid);
			if(orderForm==null){
				returnResult.setKey("error");
				returnResult.setValue("订单不存在");
			}else{
				if(!orderForm.getOrderStatus().equals("6")){
					returnResult.setKey("error");
					returnResult.setValue("订单状态不允许此操作");
				}else{
					Set set=operatorSensitiveWordService.sensitiveWordVerification(appraise);
					if(set!=null&&set.size()>0){
						returnResult.setKey("error");
						returnResult.setValue("评价内容包含敏感词汇");
					}else{
						PersonalAppraise personalAppraise=new PersonalAppraise();
						personalAppraise.setInfo(info);
						personalAppraise.setAppraise(appraise);
						personalAppraise.setOrderForm(orderForm);
						personalAppraise.setAppraiseTime(new Date());
						personalAppraise.setCommodityId(orderForm.getCommodity().getId());
						String[] infos=info.split(",");
						if(infos.length>0){
							Double sum=(double) 0;
							for (int i = 0; i < infos.length; i++) {
								String str=infos[i];
								String[] strs=str.split("@");
								sum+=Double.parseDouble(strs[1]);
							}
							personalAppraise.setAverageValue(new BigDecimal(sum/infos.length).setScale(1, BigDecimal.ROUND_HALF_UP).doubleValue());
						}
						personalAppraiseRepository.save(personalAppraise);
						orderForm.setOrderStatus("13");
						ProvideCommodity commodity=orderForm.getCommodity();
						int evaluateValue=0;
						if(commodity.getEvaluateValue()!=null){
							evaluateValue=commodity.getEvaluateValue();
						}
						commodity.setEvaluateValue(evaluateValue);
						provideCommodityRepository.save(commodity);
						personalOrderFormRepository.save(orderForm);
						
						MessageType messageType=new MessageType();
						messageType.setType(3);
						messageType.setSourceid(orderForm.getId());
						messageType.setIsread(0);
						messageType.setDecribestr("有用户对【"+orderForm.getOrderNumber()+"】做出评价，请您注意查看。");
						messageType.setCreatedate(new Date());
						messageType.setBusinessid(orderForm.getCommodity().getProviderBusiness().getId());
						messageTypeRepository.save(messageType);
						returnResult.setKey("success");
						returnResult.setValue("评价成功");
					}
				}
			}
		}
		return returnResult;
	}

}
